<?php
// Security. Order is allow, deny.
$oAllowedViews = array(
	'user-login',
);
if (!@$_SESSION['rid'] && DATABASE_OK === true) {
	if (!in_array(@$_REQUEST['sView'], $oAllowedViews)) {
		header('MiJob-Message: needlogin');
		header('Location: index.php?sView=user-login&sRequestURI='.urlencode($_SERVER['REQUEST_URI']));
		exit();
	}
}
unset($oAllowedViews);

if (JobHandler::publicProjectsExist()) {
	$oPageMap[] = array(
		'type' => 'link',
		'caption' => msg('Project center', true),
		'view' => 'job-public',
		'icon' => 'job',
	);
	$oPageMap[] = array(
		'type' => 'sep',
	);
}
$oPageMap[] = array(
	'type' => 'link',
	'caption' => msg('Projects', true),
	'view' => 'job-list',
	'icon' => 'job',
);
$oPageMap[] = array(
	'type' => 'link',
	'caption' => msg('Times', true),
	'view' => 'time-list',
	'icon' => 'time',
);
$oPageMap[] = array(
	'type' => 'link',
	'caption' => msg('Clients', true),
	'view' => 'client-list',
	'icon' => 'client',
);
$oPageMap[] = array(
	'type' => 'link',
	'caption' => msg('Attendance', true),
	'view' => 'user-attendance',
	'icon' => 'user',
);
$oPageMap[] = array(
	'type' => 'sep',
);
$oPageMap[] = array(
	'type' => 'link',
	'caption' => msg('Search', true),
	'view' => 'job-search',
	'icon' => 'search',
);
$oPageMap[] = array(
	'type' => 'sep',
);
$oPageMap[] = array(
	'type' => 'link',
	'caption' => msg('Users', true),
	'view' => 'user-list',
	'icon' => 'user',
);
$oPageMap[] = array(
	'type' => 'link',
	'caption' => msg('Settings', true),
	'view' => 'appconfig-set',
	'icon' => 'user',
	'admin' => true,
);
$oPageMap[] = array(
	'type' => 'link',
	'caption' => msg('Upgrade', true),
	'view' => 'appconfig-upgrade',
	'icon' => 'user',
	'admin' => true,
);
$oPageMap[] = array(
	'type' => 'sep',
);

$oPageMap[] = array(
	'type' => 'action',
	'caption' => msg('Logout', true),
	'action' => 'user-logout',
	'icon' => 'exit',
);

function showLinks() {
	global $oPageMap;
	$oUsers = UserHandler::getUsers();
	if (@$_SESSION['rid']) {
		foreach ($oPageMap as $oThisPage) {
			if (@$oThisPage['admin'] && !$oUsers[$_SESSION['rid']]->bAdmin) {
				continue;
			}
			switch ($oThisPage['type']) {
				case 'link':
					echo '<div class="frame"><img src="images/'.$_SESSION['oUserVars']['images'].'/'.$oThisPage['icon'].'.png" border="0"> <a href="index.php?sView='.$oThisPage['view'].'">'.$oThisPage['caption'].'</a></div>';
					break;
				case 'sep':
					echo '<br>';
					break;
				case 'action':
					echo '<div class="frame"><img src="images/'.$_SESSION['oUserVars']['images'].'/'.$oThisPage['icon'].'.png" border="0"> <a href="index.php?'.(@$_REQUEST['sView'] ? 'sView='.$_REQUEST['sView'].'&' : '').'sAction='.$oThisPage['action'].'">'.$oThisPage['caption'].'</a></div>';
					break;
			}
		}
	}
}

if (@$_SERVER['HTTP_MIJOB_PROTOCOL'] || false) { // "|| false" turned true enables XML-debug
	list($sClass, $sBox) = split('-', $_REQUEST['sView']);
	header('Content-Type: text/xml; charset=UTF-8');
	ob_start();
	print "<?phpxml version=\"1.0\" encoding=\"UTF-8\"?>\n";
	print "<mijob>\n";
	/*
	print "\t<headers>\n";
	foreach ($_SERVER as $sKey => $sValue) {
		print "\t\t<var key=\"".$sKey."\">".$sValue."</var>\n";
	}
	print "\t</headers>\n";
	*/
	$sFilename = 'include/classes/'.$sClass.'/xml/'.$sClass.'.'.$sBox.'.xml.php';
	if (is_file($sFilename)) {
		require($sFilename);
	}
	else {
		print "\t<error>Invalid view</error>\n";
	}
	unset($sFilename);
	print "</mijob>\n";
	$sResult = ob_get_contents();
	ob_end_clean();
	$sResult = mb_convert_encoding($sResult, 'ASCII');
//	$sResult = str_replace('<', '&lt;', $sResult);
//	$sResult = str_replacE('>', '&gt;', $sResult);
//	print '<pre>';
	print $sResult;
	exit();
}
else {
	header('Content-Type: text/html; charset=ISO-8859-1');
	header('Cache-Control: no-store, no-cache, must-revalidate');
	header('Cache-Control: post-check=0, pre-check=0', false);
	header('Pragma: no-cache');

	// Check if it's a SAJAX request
	if (@is_array($_REQUEST['rsargs'])) {
		require('include/uibase/dialog.ui.php');
	}
	// Check if it's a jQuery window request
	else if (array_key_exists('window', $_REQUEST)) {
		require('include/uibase/window.ui.php');
	}
	else {
		$_SESSION['sLastBigView'] = @$_REQUEST['sView'];
		require('include/uibase/base.ui.php');
	}
}
?>
